Imports WinXound_Net.gVar


Public Class FormPlayer

    Private WithEvents mPlayer As New wxPlayer
    Private mLastFileName As String

    Private HH As Int32 = 0
    Private MM As Int32 = 0
    Private SS As Int32 = 0

    Private bPlaying As Boolean = False


    Private Sub FormPlayer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub FormPlayer_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        ButtonStop.PerformClick()
    End Sub

    Private Sub FormPlayer_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            ButtonExit.PerformClick()
        ElseIf e.KeyCode = Keys.P Then
            ButtonPlay.PerformClick()
        ElseIf e.KeyCode = Keys.U Then
            ButtonPause.PerformClick()
        ElseIf e.KeyCode = Keys.S Then
            ButtonStop.PerformClick()
        ElseIf e.KeyCode = Keys.W Then
            ButtonWaveEditor.PerformClick()
        ElseIf e.KeyCode = Keys.B Then
            ButtonBrowseSoundFile.PerformClick()
        End If
    End Sub

    Public Shadows Sub Show()
        MyBase.Show()
        FormPlayerShow()
    End Sub

    Private Sub FormPlayerShow()

        If mLastFileName <> "" Then

            Dim ret As Boolean = mPlayer.OpenFile(mLastFileName)
            If ret Then
                Me.Text = " WinXound Sound Player [TT: " & DisplayTime(mPlayer.Duration) & "]"
                TrackBarPosition.Maximum = mPlayer.Duration
                TrackBarPosition.TickFrequency = TrackBarPosition.Maximum \ 20
                ButtonPlay.Enabled = True
                ButtonPause.Enabled = False
                ButtonStop.Enabled = False
                TrackBarPosition.Enabled = True
                ButtonWaveEditor.Enabled = True
                Exit Sub
            End If

        End If

        TrackBarPosition.Enabled = False
        ButtonPlay.Enabled = False
        ButtonPause.Enabled = False
        ButtonStop.Enabled = False
        ButtonWaveEditor.Enabled = False
        Me.Text = " WinXound Sound Player"
        TrackBarPosition.Maximum = 1
        TrackBarPosition.TickFrequency = 0
        TextBoxFileName.Text = ""



    End Sub

    Public Sub LoadFile(ByVal mFileName As String)
        ''mPlayer.OpenFile(mFileName)
        mLastFileName = mFileName
        TextBoxFileName.Text = mLastFileName
        FormPlayerShow()
    End Sub

    Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click
        mPlayer.StopSound()
    End Sub

    Private Sub ButtonPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPlay.Click
        mPlayer.PlaySound()
        ButtonPlay.Enabled = False
        ButtonPause.Enabled = True
        ButtonStop.Enabled = True
    End Sub

    Private Sub ButtonPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPause.Click
        mPlayer.PauseSound()
    End Sub



    Private Sub mPlayer_Player_Paused() Handles mPlayer.Player_Paused
        ButtonPause.Enabled = False
        ButtonPlay.Enabled = True
        ButtonStop.Enabled = True
    End Sub

    Private Sub mPlayer_Player_Stopped() Handles mPlayer.Player_Stopped
        LabelPosition.Text = "00:00:00"
        TrackBarPosition.Value = 0
        ButtonPlay.Enabled = True
        ButtonPause.Enabled = False
        ButtonStop.Enabled = False
    End Sub

    Private Sub mPlayer_Player_TimerTick() Handles mPlayer.Player_TimerTick
        LabelPosition.Text = DisplayTime(mPlayer.Position)
        TrackBarPosition.Value = mPlayer.Position
    End Sub




    Private Function DisplayTime(ByVal mMilliseconds As Int32) As String
        HH = mMilliseconds \ 1000 \ 60 \ 60
        MM = mMilliseconds \ 1000 \ 60
        SS = mMilliseconds \ 1000 Mod 60

        Dim tempString As String = Strings.Format(HH, "00") & ":" & _
                                   Strings.Format(MM, "00") & ":" & _
                                   Strings.Format(SS, "00")

        Return tempString
    End Function

    Private Sub TrackBarPosition_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrackBarPosition.MouseDown
        If mPlayer.IsPlaying Then bPlaying = True
        ButtonPause.PerformClick()
    End Sub

    Private Sub TrackBarPosition_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBarPosition.Scroll
        mPlayer.Position = TrackBarPosition.Value
        LabelPosition.Text = DisplayTime(mPlayer.Position)
    End Sub

    Private Sub TrackBarPosition_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrackBarPosition.MouseUp
        If bPlaying Then
            ButtonPlay.PerformClick()
        End If
        bPlaying = False
    End Sub



    Private Sub ButtonExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExit.Click
        ButtonStop.PerformClick()
        mPlayer.Dispose()
        Me.Hide()
        ''Me.Close()
    End Sub

    Private Sub ButtonBrowseSoundFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowseSoundFile.Click
        Dim openFileDialog1 As New OpenFileDialog

        openFileDialog1.Filter = "Wav file (*.wav)|*.wav|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True

        If Not (openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Or _
                openFileDialog1.FileName = vbNullString Then
            Exit Sub
        End If

        Me.LoadFile(openFileDialog1.FileName)
        openFileDialog1.Dispose()
    End Sub


    Private Sub ButtonWaveEditor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonWaveEditor.Click
        Try
            If Preferences.Directory.WaveEditor <> "" Then
                If System.IO.File.Exists(Preferences.Directory.WaveEditor) Then
                    If mLastFileName <> "" Then
                        If System.IO.File.Exists(mLastFileName) Then
                            System.Diagnostics.Process.Start(Preferences.Directory.WaveEditor, _
                                                             """" & mLastFileName & """")
                        End If
                    Else
                        System.Diagnostics.Process.Start(Preferences.Directory.WaveEditor)
                    End If
                    ButtonExit.PerformClick()
                Else
                    Throw New System.Exception("")
                End If
            Else
                Throw New System.Exception("")
            End If

        Catch ex As Exception
            MessageBox.Show("Unable to start Wave Editor executable!" & vbCrLf & _
                            "Please check WinXound Settings (Directories->Wave Editor Executable field)", _
                            "WinXound Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub


End Class